package top.hmtools.eureka.client.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import top.hmtools.eureka.client.domain.UserBean;

@RestController
public class ServiceSampleController {
	
	private final Logger logger = LoggerFactory.getLogger(ServiceSampleController.class);
	
	@Value("${project.name:}")
	String projectName;
	
	@Value("${project.version:}")
	String projectVersion;
	
	@Value("${server.port}")
	int serverPort;

	/**
	 * 测试用的controller，作为服务提供者 被 消费者（比如：feign）调用，其中服务提供者、消费者 均是 Eureka 客户端。
	 * @return
	 */
	@RequestMapping(value="/hi",method=RequestMethod.GET,produces={MediaType.TEXT_HTML_VALUE})
	public String sayHi(){
		this.logger.info("top.hmtools.eureka.client.controller.ServiceSampleController.sayHi()  执行》》》");
		return "eruka client say : Hi!Now is "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" and i am "+this.projectName+"("+this.projectVersion+") : "+this.serverPort;
	}
	
	/**
	 * 返回数据类型为： ResponseEntity<UserBean>
	 * @return
	 */
	@RequestMapping(value="/re",method=RequestMethod.GET)
	public ResponseEntity<UserBean> re(){
		UserBean ub = new UserBean();
		ub.setAge(22);
		ub.setName("luFei");
		ResponseEntity<UserBean> result = new ResponseEntity<>(ub,HttpStatus.OK);
		return result;
	}
	
	/**
	 * 返回数据类型为： ResponseEntity<List<UserBean>>
	 * @return
	 */
	@RequestMapping(value="/res",method=RequestMethod.GET)
	public ResponseEntity<List<UserBean>> res(){
		UserBean ubLuFei = new UserBean();
		ubLuFei.setAge(22);
		ubLuFei.setName("luFei");
		
		UserBean ubSuoLong = new UserBean();
		ubSuoLong.setAge(24);
		ubSuoLong.setName("SuoLong");
		
		UserBean ubSanJi = new UserBean();
		ubSanJi.setAge(21);
		ubSanJi.setName("SanJi");
		
		List<UserBean> ubs = new ArrayList<>();
		ubs.add(ubLuFei);
		ubs.add(ubSanJi);
		ubs.add(ubSuoLong);
		ResponseEntity<List<UserBean>> result = new ResponseEntity<>(ubs,HttpStatus.OK);
		return result;
	}
	
	/**
	 * 返回数据类型为：ModelAndView
	 * @return
	 */
	@RequestMapping(value="/mav",method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView mav(){
		UserBean ub = new UserBean();
		ub.setAge(33);
		ub.setName("maTeng");
		ModelAndView mav = new ModelAndView();
		mav.addObject("",ub);
		return mav;
	}
}
